package com.nbsaas.boot.script.controller;

import com.nbsaas.boot.rest.response.ResponseObject;
import com.nbsaas.boot.script.api.apis.ScriptTemplateApi;
import com.nbsaas.boot.script.ext.domain.request.ScriptRequest;
import com.nbsaas.boot.script.ext.resource.HotReloadScriptEngine;
import com.nbsaas.boot.script.ext.resource.SpringScriptSource;
import com.nbsaas.boot.script.rest.resource.ScriptTemplateResource;
import jakarta.annotation.Resource;
import lombok.Getter;
import lombok.Setter;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/script")
public class ScriptController {

    private final HotReloadScriptEngine scriptEngine = new HotReloadScriptEngine("groovy");

    @Resource
    private ApplicationContext applicationContext;

    @Resource
    private ScriptTemplateApi scriptTemplateApi;

    @Resource
    private SpringScriptSource springScriptSource;

    /**
     * POST /api/script/run
     * {
     *   "path": "classpath:example/userInfo.groovy",
     *   "params": {
     *       "name": "Ada",
     *       "age": 30
     *   }
     * }
     */
    @PostMapping("/run")
    public ResponseObject<Object > runScript(@RequestBody ScriptRequest request)  {
        ResponseObject<Object> result=new ResponseObject<Object>();
        if (request.getPath() == null || request.getPath().isBlank()) {
            result.setCode(501);
            result.setMsg("缺少脚本路径参数 path");
           return result;
        }
        try {
            Map<String, Object> params =new HashMap<>();
            params.put("ctx", applicationContext);
            params.put("param", request.getParams());
            Object res = scriptEngine.execute(request.getPath(), params);
            if (res instanceof ResponseObject<?>){
                @SuppressWarnings("unchecked")
                ResponseObject<Object> backRes = (ResponseObject<Object>) res;
                return backRes;
            }
            result.setData(res);
        }catch (Exception e){
            result.setCode(502);
            result.setMsg(e.getMessage());
        }
        return result;
    }
    @PostMapping("/handle")
    public ResponseObject<Object > handleScript(@RequestBody ScriptRequest request)  {
        ResponseObject<Object> result=new ResponseObject<Object>();
        if (request.getModel() == null || request.getModel().isBlank()) {
            result.setCode(501);
            result.setMsg("模块名称不能为空");
            return result;
        }
        try {
            request.setPath("model:"+request.getModel());
            Map<String, Object> params =new HashMap<>();
            params.put("ctx", applicationContext);
            params.put("param", request.getParams());
            if (!scriptEngine.hasSource("model")){
                scriptEngine.addSource(springScriptSource);
            }
            Object res = scriptEngine.execute(request.getPath(), params);
            if (res instanceof ResponseObject<?>){
                @SuppressWarnings("unchecked")
                ResponseObject<Object> backRes = (ResponseObject<Object>) res;
                return backRes;
            }
            result.setData(res);
        }catch (Exception e){
            result.setCode(502);
            result.setMsg(e.getMessage());
        }
        return result;
    }

}
